package com.poly.flowable.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.core.web.page.TableData;
import com.poly.flowable.pojo.dto.FormDTO;
import com.poly.flowable.pojo.dto.FormModelDTO;
import com.poly.flowable.pojo.query.FormQuery;
import com.poly.flowable.service.ISysFormBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程表单Controller
 *
 * @author Yang huijing
 * @date 2021-04-03
 */
@Api(tags = "表单业务管理")
@RestController
@RequestMapping("/form/biz")
public class SysFormBizController extends BaseController {

    @Autowired
    private ISysFormBizService formBizService;

    /**
     * 新增数据
     *
     * @param formId
     * @param dto
     * @return
     */
    @ApiOperation("新增数据")
    @PostMapping("/{formId}")
    public R add(@PathVariable Long formId, @RequestBody FormDTO dto) {
        return R.ok(formBizService.add(formId, dto));
    }


    /**
     * 修改数据
     *
     * @param formId
     * @param dto
     * @return
     */
    @ApiOperation("修改数据")
    @PutMapping("/{formId}")
    public R update(@PathVariable Long formId, @RequestBody FormDTO dto) {
        return R.ok(formBizService.update(formId, dto));
    }


    /**
     * 删除数据
     *
     * @param formId
     * @param idList
     * @return
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/{formId}/remove")
    public R remove(@PathVariable Long formId, @RequestBody List<Long> idList) {
        formBizService.remove(formId, idList);
        return R.ok();
    }

    /**
     * 详情
     *
     * @param formId
     * @param id
     * @return
     */
    @ApiOperation("详情")
    @GetMapping("/{formId}/{id}")
    public R<Map<String,Object>> get(@PathVariable Long formId, @PathVariable Long id) {
        return R.ok(formBizService.get(formId, id));
    }

    /**
     * 详情
     *
     * @param formId
     * @param id
     * @return
     */
    @ApiOperation("详情")
    @GetMapping("/{formId}/detail/{id}")
    public R<Map<String,Object>> detail(@PathVariable Long formId, @PathVariable Long id) {
        return R.ok(formBizService.getDetail(formId, id));
    }

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/{formId}/queryPage")
    public TableData<Map<String, Object>> queryPage(@PathVariable Long formId, FormQuery query) {
        return formBizService.queryPage(formId, query);
    }

    /**
     * 获取表单配置
     *
     * @return
     */
    @ApiOperation("获取表单配置")
    @GetMapping("/{formId}/config")
    public R<FormModelDTO> queryFormConfig(@PathVariable Long formId) {
        return R.ok(formBizService.getFormConfig(formId));
    }

    /**
     * 获取表单配置
     *
     * @return
     */
    @ApiOperation("获取表单配置")
    @GetMapping("/{formId}/model")
    public R queryFormModel(@PathVariable Long formId) {
        return R.ok(formBizService.getFormModel(formId));
    }


    /**
     * 启动流程实例
     *
     * @return
     */
    @ApiOperation("启动流程实例")
    @PostMapping("/{formId}/start")
    public R start(@PathVariable Long formId, @RequestBody FormDTO dto) {
        return R.ok(formBizService.startProcess(formId, dto));
    }

}
